<template>
  <div v-if="visible" class="modal-overlay" @click="handleOverlayClick">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>文章详情</h3>
        <button @click="closeModal" class="close-btn">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="18" y1="6" x2="6" y2="18"></line>
            <line x1="6" y1="6" x2="18" y2="18"></line>
          </svg>
        </button>
      </div>
      
      <div class="modal-body">
        <div class="article-info">
          <div class="info-row">
            <span class="label">标题：</span>
            <span class="value">{{ article.title }}</span>
          </div>
          
          <div class="info-row">
            <span class="label">来源：</span>
            <span class="value">{{ article.from || '未知来源' }}</span>
          </div>
          
          <div class="info-row">
            <span class="label">品牌：</span>
            <span class="value">{{ article.brand || '未知品牌' }}</span>
          </div>
          
          <div class="info-row">
            <span class="label">发布时间：</span>
            <span class="value">{{ formatDate(article.create_time) }}</span>
          </div>
        </div>
        
        <div class="article-content" v-if="article.content">
          <h4>文章内容</h4>
          <div class="content-text" v-html="formatContent(article.content)"></div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button v-if="!isAdded" @click="confirmAdd" class="confirm-btn">确认添加</button>
        <button @click="closeModal" class="cancel-btn">{{ isAdded ? '关闭' : '取消' }}</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  article: {
    type: Object,
    default: () => ({})
  },
  isAdded: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['close', 'confirm']);

const closeModal = () => {
  emit('close');
};

const confirmAdd = () => {
  emit('confirm', props.article);
};

const handleOverlayClick = () => {
  closeModal();
};

const formatDate = (dateString) => {
  if (!dateString) return '未知时间';
  
  try {
    const date = new Date(dateString);
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: 'long',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit'
    });
  } catch (error) {
    return dateString;
  }
};

const formatContent = (content) => {
  if (!content) return '';

  const wrappedContent = smartTextWrap(content);
  
  // 将换行符转换为HTML的<br>标签
  return wrappedContent
    .replace(/\n/g, '<br>')
    .replace(/\r\n/g, '<br>')
    .replace(/\r/g, '<br>');
};

const smartTextWrap = (text, maxLineLength = 200) => {
  // 保留原有的分段结构
  const paragraphs = text.split(/(PART\.\d+|编辑：|－商务合作－)/);
  
  return paragraphs.map(paragraph => {
    // 特殊段落不处理（如PART标记、编辑信息等）
    if (/^(PART\.\d+|编辑：|－商务合作－)/.test(paragraph)) {
      return '\n' + paragraph.trim();
    }
    
    // 处理常规段落
    let result = [];
    let currentLine = '';
    
    // 按句子分割（中文句号、感叹号、问号）
    const sentences = paragraph.split(/([。！？])\s*/);
    
    for (let i = 0; i < sentences.length; i += 2) {
      const sentence = (sentences[i] + (sentences[i+1] || '')).trim();
      if (!sentence) continue;
      
      // 情况1：当前行+新句子不超过最大长度
      if (currentLine.length + sentence.length <= maxLineLength) {
        currentLine += sentence;
      } 
      // 情况2：需要换行处理
      else {
        // 优先尝试在句子结尾处换行
        if (currentLine.length > 0) {
          result.push(currentLine);
          currentLine = sentence;
        } 
        // 当前行空且单句超长时强制分割
        else {
          let remaining = sentence;
          while (remaining.length > maxLineLength) {
            // 查找最后一个标点位置
            const lastPunct = remaining.substring(0, maxLineLength).lastIndexOf(/[。！？]/);
            const splitPos = lastPunct > 0 ? lastPunct : maxLineLength;
            
            result.push(remaining.substring(0, splitPos));
            remaining = remaining.substring(splitPos);
          }
          currentLine = remaining;
        }
      }
    }
    
    if (currentLine) result.push(currentLine);
    return result.join('\n\n');
  }).join('\n\n');
}


</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.5) 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(8px);
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-content {
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
  border-radius: 16px;
  width: 95%;
  max-width: 900px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 
    0 20px 60px rgba(0, 0, 0, 0.15),
    0 8px 25px rgba(0, 0, 0, 0.1),
    0 0 0 1px rgba(255, 255, 255, 0.1);
  animation: slideIn 0.3s ease-out;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

@keyframes slideIn {
  from {
    transform: translateY(-30px) scale(0.95);
    opacity: 0;
  }
  to {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  background: #007bff;
  border-radius: 16px 16px 0 0;
  color: white;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.4em;
  font-weight: 600;
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.close-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  color: white;
  transition: all 0.3s ease;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.modal-body {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background: white;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-info {
  flex: 0 0 auto;
}

.info-row {
  display: flex;
  margin-bottom: 16px;
  align-items: flex-start;
  padding: 10px 14px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  transition: all 0.3s ease;
}

.info-row:hover {
  background: #e9ecef;
  border-color: #007bff;
}

.label {
  font-weight: 600;
  color: #2c3e50;
  min-width: 80px;
  margin-right: 12px;
  font-size: 0.9em;
}

.value {
  flex: 1;
  color: #2c3e50;
  line-height: 1.6;
  font-size: 0.95em;
}

.summary {
  font-style: italic;
  color: #6c757d;
  background: rgba(0, 123, 255, 0.05);
  padding: 8px 12px;
  border-radius: 6px;
  border-left: 3px solid #007bff;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tag {
  background: #007bff;
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 0.8em;
  font-weight: 500;
  transition: all 0.3s ease;
}

.tag:hover {
  background: #0056b3;
}

.article-content {
  flex: 1;
  border-top: 1px solid #e9ecef;
  padding-top: 20px;
}

.article-content h4 {
  margin: 0 0 12px 0;
  color: #2c3e50;
  font-size: 1.1em;
  font-weight: 600;
}

.content-text {
  color: #2c3e50;
  line-height: 1.6;
  height: 400px;
  overflow-y: auto;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  font-size: 14px;
}

.content-text::-webkit-scrollbar {
  width: 6px;
}

.content-text::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.content-text::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.content-text::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid #e9ecef;
  background: #f8f9fa;
  border-radius: 0 0 16px 16px;
}

.confirm-btn, .cancel-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: background-color 0.3s ease;
  min-width: 80px;
}

.confirm-btn {
  background: #007bff;
  color: white;
}

.confirm-btn:hover {
  background: #0056b3;
}

.cancel-btn {
  background: #6c757d;
  color: white;
}

.cancel-btn:hover {
  background: #5a6268;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-content {
    width: 98%;
    max-height: 95vh;
    border-radius: 12px;
  }
  
  .modal-header {
    padding: 16px 20px 12px;
    border-radius: 12px 12px 0 0;
  }
  
  .modal-body {
    padding: 16px;
  }
  
  .modal-footer {
    padding: 16px 20px;
    border-radius: 0 0 12px 12px;
  }
  
  .info-row {
    flex-direction: column;
    gap: 6px;
  }
  
  .label {
    min-width: auto;
    margin-right: 0;
  }
  
  .content-text {
    height: 300px;
  }
  
  .confirm-btn, .cancel-btn {
    padding: 8px 16px;
    font-size: 13px;
  }
}
</style> 